package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * admin敏感词service接口的实现类
 */
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    //根据敏感词模糊搜索并分页表示
    @Override
    public ResponseResult sensitiveWordFindByNameAndPage(SensitiveDto dto) {
        //判断参数是否为空
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //分页查询条件
        Page<AdSensitive> page =new Page(dto.getPage(),dto.getSize());

        //LambdaQueryWrapper 可以通过(实体类::get字段名，值) 传入搜索条件
        LambdaQueryWrapper<AdSensitive>queryWrapper= new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        //分页查询
        IPage<AdSensitive> pageResult = this.page(page, queryWrapper);

        ResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal());
        result.setData(pageResult.getRecords());
        return result;

    }
    //新增敏感词
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //判断参数是否为空
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //补全参数
        adSensitive.setCreatedTime(new Date());
        boolean flag = this.save(adSensitive);
        //根据返回值判断有没有添加成功
        if (flag){
            return ResponseResult.okResult();
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
    //根据id更新敏感词
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //判断参数
        if (adSensitive.getId()==null||adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //根据返回值判断有没有更新成功
        boolean flag = this.updateById(adSensitive);
        if (flag){
            return ResponseResult.okResult();
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    //根据id删除敏感词
    @Override
    public ResponseResult deleteById(Integer id) {
        //判断参数
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        boolean flag = this.removeById(id);
        //根据返回值判断有没有更新成功
        if (flag){
            return ResponseResult.okResult();
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

}
